package hiatao.puzzle.adapter;

import haitao.puzzle.R;
import hiatao.puzzle.entity.User;
import hiatao.puzzle.utils.ImageUtils;
import hiatao.puzzle.utils.Tools;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListUserAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<User> userList =new ArrayList<User>();
	private Context context;
	
	public ListUserAdapter(Context context,  List<User> newsList) {
		if(newsList!=null){
			this.userList = newsList;
		}
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		
	}

	@Override
	public int getCount() {
		return userList==null ? 0:userList.size() ;
	}

	@Override
	public Object getItem(int position) {
		return userList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void appendToList(List<User> addList) {

		if (addList == null) {
			return;
		}
		if (userList == null) {
			userList = new ArrayList<User>();
		}
		userList.addAll(addList);
		notifyDataSetChanged();
	}
	
	public void clearAll() {
	    userList.clear();
	    notifyDataSetChanged();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PicModelHodler holder ;
		User user = userList.get(position);
		if(convertView == null){
			holder = new PicModelHodler();
			convertView = mInflater.inflate(R.layout.list_user, parent,false);
			holder.image = (ImageView) convertView.findViewById(R.id.head_image);
			//holder.rank_img = (ImageView) convertView.findViewById(R.id.rank_img);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.zuan_tv = (TextView) convertView.findViewById(R.id.zuan_tv);
			holder.rank_tv = (TextView) convertView.findViewById(R.id.rank_tv);
			holder.intro_tv = (TextView) convertView.findViewById(R.id.intro_tv);
			convertView.setTag(holder);
		}
		else{
			holder = (PicModelHodler) convertView.getTag();
		}
		ImageUtils.loadHeadImg(user.getHeadUrl(), holder.image);
		holder.name_tv.setText(user.getName());
		holder.intro_tv.setText(user.getIntro());
		holder.rank_tv.setText(""+(position+1));
		holder.zuan_tv.setText(""+user.getZuanNum()+"分");
		
/*		if(position==0){
			holder.rank_img.setImageResource(R.drawable.ic_zuan_yellow);
		}else if(position==1) {
			holder.rank_img.setImageResource(R.drawable.ic_zuan_green);
		}else{
			holder.rank_img.setImageResource(R.drawable.ic_zuan_red);
		}
		*/
		/*if(!user.isFocus()){
			holder.follow_ly.setBackgroundResource(R.drawable.un_follow);
			holder.sixin_tv.setTextColor(context.getResources().getColor(R.color.head_color));
			holder.follow_tv.setTextColor(context.getResources().getColor(R.color.head_color));
			holder.follow_tv.setText("+关注");
		}else{
			holder.follow_ly.setBackgroundResource(R.drawable.has_follow);
			holder.sixin_tv.setTextColor(context.getResources().getColor(R.color.follow_green));
			holder.follow_tv.setTextColor(context.getResources().getColor(R.color.white));
			holder.follow_tv.setText("已关注");
		}*/
		/*holder.start_date.setText(user.getStartDate());
		holder.left_date.setText(user.getLeftDate());
		holder.left_date.setText(user.getLeftDate());*/
/*		FileUtils.setLoacalImg(book.getPicuri(), holder.imageView);
		holder.author.setText(book.getAuthor());
		holder.bookname.setText(book.getName());
		holder.start_date.setText(book.getStartDate());
		holder.left_date.setText(book.getLeftDate());
		holder.left_date.setText(book.getLeftDate());
		final TextView playTv =holder.play;
		if(book.getState()==User.OUT_OF_DATE){
			holder.play.setBackgroundResource(R.drawable.button_red_selector);
			holder.play.setText("超期  请归还");
		}else if (book.getState()==User.NORMAL){
			holder.play.setBackgroundResource(R.drawable.button_green_selector);
			holder.play.setText("续借");
			if("续借".equals(holder.play.getText().toString())){
				holder.play.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						playTv.setText("续借中...");
						playTv.setClickable(false);
						new Handler().postDelayed(new Runnable() {
							
							@Override
							public void run() {
								playTv.setText("已续借");
								playTv.setBackgroundResource(R.drawable.button_gray_selector);
							}
						}, 1000);
					}
				});
			}
			
		}else{
			holder.play.setBackgroundResource(R.drawable.button_gray_selector);
			holder.play.setText("已续借");
			
		}*/
		return convertView;
	}

	private final class PicModelHodler {
		public ImageView image;
		//public ImageView rank_img;
		public TextView name_tv;
		public TextView zuan_tv;
		public TextView rank_tv;
		public TextView intro_tv;
	}
}
